package login

import (
	"gonexample/gone/e"
	"gonexample/libs/helper"
	"net/http"
	"strings"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

// PcLogin 登录
func PcLogin(c *gin.Context) {
	session := sessions.Default(c)

	rawUserId := c.Query("setUserId")
	pass := c.Query("pass")

	if rawUserId == "" || pass == "" {
		c.JSON(http.StatusOK, e.New(e.ErrParamsEmpty))
		return
	}

	uid := helper.InterfaceToInt(rawUserId)
	if uid <= 0 {
		c.JSON(http.StatusOK, e.New(e.ErrParamsInvalid))
		return
	}

	if pass != "helloa12334" {
		c.JSON(http.StatusOK, e.New(e.ErrParamsInvalid))
		return
	}

	session.Set("uid", uid)
	session.Save()

	t := c.Writer.Header().Get("Set-Cookie")
	if t == "" {
		c.JSON(http.StatusOK, e.New(e.ErrNotFound))
		return
	}

	c.JSON(http.StatusOK, e.New(e.OK).SetData(t[:strings.Index(t, ";")]))
	return
}
